import dayjs from 'dayjs'

/**
 * 数组转树形结构
 * @param {array} list 被转换的数组
 * @param {number|string} root 根节点（最外层节点）
 * @returns array
 */
function arrayToTree(list, root_key = 'id', root = 0, rootName = 'pid') {
	return list
		.filter(item => item[rootName] === root)
		.map(item => ({...item, children: arrayToTree(list, root_key, item[root_key])}))
}

function getBase64(file) {
	return new Promise((resolve, reject) => {
		const reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = () => resolve(reader.result);
		reader.onerror = error => reject(error);
	});
}

export default {arrayToTree, getBase64, dayjs}